//
//  WBHomeController.swift
//  czWB
//
//  Created by Qing on 2018/5/25.
//  Copyright © 2018年 成哥哥. All rights reserved.
//

import UIKit

private let cellID = "cellID"
private var arrayM = [String]()

class WBHomeController: WBBaseController {
    
    lazy var statusListViewModel = WBStatusListViewModel()
    
    override func loadData() {
        
//        print("最后一条微博内容是\(self.statusListViewModel.statusListViewModel.last?.text)")
        statusListViewModel.loadStatus (isPullup: self.isPullup){ (isSuccess, shouldRefresh) in
            
//            停止下拉菊花控件转动
            self.refreshController?.endRefreshing()
//            上拉刷新成功后更改上拉刷新标识为false
            self.isPullup = false
            if shouldRefresh {
                
//            更新表格数据
                self.tableView?.reloadData()
                
            }
            
        }
        
        
////        演示延时加载数据
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
//
////           绑定假数据
//            for i in 0..<15 {
//
////                如果未上拉刷新过，则上拉新增数据。反之则是执行下拉新增数据
//                if self.isPullup {
//
//                    arrayM.append("上拉\(i)个")
//
//                }else{
//
//                    arrayM.insert(String(i), at: 0)
//
//                }
//            }
//
////            更新表格数据
//            self.tableView?.reloadData()
////            停止下拉菊花控件转动
//            self.refreshController?.endRefreshing()
////            刷新成功后更改上拉刷新标识为false
//            self.isPullup = false
//            print("刷新表格成功")
//        }
//
    }
    
    
    //FIXME: 未完成动作
    /// Push 好友页面动作
    @objc private func showFriends()  {
        
        let vc = WBDemoViewController()

        navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc private func showNext()  {
        
        let vc = WBDemoViewController()
        
        navigationController?.pushViewController(vc, animated: true)
        
    }
}


// MARK: - 数据源方法
extension WBHomeController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statusListViewModel.statusViewModel.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WBStatusCell

        let vm = self.statusListViewModel.statusViewModel[indexPath.row]
        
//        传值给WBStatusCell控制器
        cell.StatusViewModel = vm
        
        return cell
    }


}


// MARK: - 设置界面
extension WBHomeController {
    
    /// 设置UI
     override func setTableView() {
        
        super.setTableView()
        
//        设置左边BarButton
        navItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", target: self, action: #selector(WBHomeController.showFriends))

//       注册可重用cell
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil),
                            forCellReuseIdentifier: cellID)
        
//        自动计算行高
        tableView?.rowHeight = UITableView.automaticDimension
//        预测行高是多少
        tableView?.estimatedRowHeight = 300
        
//        取消表格分割线
        tableView?.separatorStyle = .none
        
        setupTitleView()
        
    }
    
    /// 在首页显示图片和用户昵称
    private func setupTitleView()  {
        
        let title = WBNetworkManager.shared.userAcount.screen_name
        
        let btn = WBTitleButton(title: title)

        btn.addTarget(self, action: #selector(clickTitleBtn), for: UIControl.Event.touchDown)
        
        navItem.titleView = btn
        
    }
    
    
    /// 点击title动作，切的箭头上下动作
    @objc func clickTitleBtn(btn: UIButton)  {
        
        btn.isSelected = !btn.isSelected
        
    }

}


